package grape.crm.rest.customerrelation.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.InvalidParamsException;
import grape.common.exception.runtime.RDataNotExistException;
import grape.crm.service.customer.api.ICustomerService;
import grape.crm.service.customer.po.Customer;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.crm.rest.customerrelation.form.CustomerRelationCreateForm;
import grape.crm.rest.customerrelation.form.CustomerRelationUpdateForm;
import grape.crm.rest.customerrelation.form.CustomerRelationListPageForm;
import grape.crm.rest.customerrelation.vo.CustomerRelationVo;
import grape.crm.rest.customerrelation.mapper.CustomerRelationWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.crm.service.customerrelation.po.CustomerRelation;
import grape.crm.service.customerrelation.api.ICustomerRelationService;
import java.util.List;
/**
 * <p>
 * 客户关系 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
@RestController
@RequestMapping("/crm/customerrelation")
@Api(tags = "客户关系相关接口")
public class CustomerRelationController extends BaseController<CustomerRelationVo, CustomerRelation> {

    @Autowired
    private CustomerRelationWebMapper currentWebMapper;
    @Autowired
    private ICustomerRelationService currentService;

    @Autowired
    private ICustomerService iCustomerService;




     @ApiOperation("添加客户关系")
     @PreAuthorize("hasAuthority('customerrelation:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public CustomerRelationVo create(@RequestBody @Valid CustomerRelationCreateForm cf) {
         CustomerRelation po = currentWebMapper.formToPo(cf);
         Customer byCode = iCustomerService.getByCode(cf.getCustomerCode());
         if (byCode == null) {
             throw new InvalidParamsException("客户编码不存在");
         }
         po.setCustomerId(byCode.getId());
         byCode = iCustomerService.getByCode(cf.getAnotherCustomerCode());
         if (byCode == null) {
             throw new InvalidParamsException("另一个客户编码不存在");
         }
         po.setAnotherCustomerId(byCode.getId());

         return super.create(po);
     }

     @ApiOperation("根据ID查询客户关系")
     @PreAuthorize("hasAuthority('customerrelation:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public CustomerRelationVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除客户关系")
     @PreAuthorize("hasAuthority('customerrelation:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新客户关系")
     @PreAuthorize("hasAuthority('customerrelation:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public CustomerRelationVo update(@PathVariable String id,@RequestBody @Valid CustomerRelationUpdateForm uf) {
         CustomerRelation po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("分页查询客户关系")
    @PreAuthorize("hasAuthority('customerrelation:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<CustomerRelationVo> listPage(CustomerRelationListPageForm listPageForm) {
         CustomerRelation po = currentWebMapper.formToPo(listPageForm);
        if(!isStrEmpty(listPageForm.getCustomerCode())){
            Customer byCode = iCustomerService.getByCode(listPageForm.getCustomerCode());
            if (byCode == null) {
                throw new RDataNotExistException("暂无数据");
            }
            po.setCustomerId(byCode.getId());

        }
        if(!isStrEmpty(listPageForm.getAnotherCustomerCode())){
            Customer byCode = iCustomerService.getByCode(listPageForm.getAnotherCustomerCode());
            if (byCode == null) {
                throw new RDataNotExistException("暂无数据");
            }
            po.setAnotherCustomerId(byCode.getId());

        }

         return super.listPage(po,listPageForm);
     }

}
